<template>
  <div class="min-h-screen bg-gray-50">
    <!-- 侧边栏 -->
    <el-aside 
      :width="isCollapsed ? '64px' : '240px'" 
      class="transition-all duration-300 bg-white shadow-lg"
    >
      <div class="h-16 flex items-center justify-center border-b border-gray-200">
        <div v-if="!isCollapsed" class="flex items-center space-x-2">
          <el-icon class="text-2xl text-blue-600"><Car /></el-icon>
          <span class="text-lg font-bold text-gray-800">车辆管理系统</span>
        </div>
        <el-icon v-else class="text-2xl text-blue-600"><Car /></el-icon>
      </div>
      
      <el-menu 
        :default-active="$route.path"
        :collapse="isCollapsed"
        :router="true"
        class="border-none"
      >
        <el-menu-item index="/dashboard">
          <el-icon><Odometer /></el-icon>
          <template #title>仪表板</template>
        </el-menu-item>
        
        <el-menu-item index="/vehicles">
          <el-icon><Car /></el-icon>
          <template #title>车辆管理</template>
        </el-menu-item>
        
        <el-menu-item index="/applications">
          <el-icon><Document /></el-icon>
          <template #title>申请管理</template>
        </el-menu-item>
        
        <el-menu-item index="/drivers">
          <el-icon><User /></el-icon>
          <template #title>驾驶员管理</template>
        </el-menu-item>
        
        <el-menu-item index="/expenses">
          <el-icon><Money /></el-icon>
          <template #title>费用管理</template>
        </el-menu-item>
        
        <el-menu-item index="/settings">
          <el-icon><Setting /></el-icon>
          <template #title>系统设置</template>
        </el-menu-item>
      </el-menu>
      
      <!-- 版本信息 -->
      <div v-if="!isCollapsed" class="absolute bottom-4 left-4 text-xs text-gray-400">
        车辆管理系统 v1.0.0
      </div>
    </el-aside>
    
    <!-- 主内容区 -->
    <el-container class="ml-0">
      <!-- 顶部导航 -->
      <el-header class="bg-white shadow-sm border-b border-gray-200 px-6 flex items-center justify-between">
        <div class="flex items-center space-x-4">
          <el-button 
            :icon="isCollapsed ? Expand : Fold" 
            @click="toggleSidebar"
            text
            class="text-gray-600"
          />
          
          <!-- 搜索框 -->
          <el-input
            v-model="searchQuery"
            placeholder="搜索车牌号、申请、驾驶员..."
            :prefix-icon="Search"
            class="w-80"
            clearable
          />
        </div>
        
        <div class="flex items-center space-x-4">
          <!-- 主题切换 -->
          <el-button :icon="isDark ? Sunny : Moon" @click="toggleTheme" text />
          
          <!-- 通知 -->
          <el-badge :value="3" class="item">
            <el-button :icon="Bell" text />
          </el-badge>
          
          <!-- 用户菜单 -->
          <el-dropdown @command="handleUserCommand">
            <div class="flex items-center space-x-2 cursor-pointer hover:bg-gray-50 px-3 py-2 rounded">
              <el-avatar :size="32" :src="userStore.user?.avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <span class="text-sm font-medium">{{ userStore.user?.name }}</span>
              <el-icon class="text-gray-400"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人资料
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 主内容 -->
      <el-main class="p-6">
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import {
  Car,
  Odometer,
  Document,
  User,
  Money,
  Setting,
  Expand,
  Fold,
  Search,
  Bell,
  Sunny,
  Moon,
  ArrowDown,
  SwitchButton
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const isCollapsed = ref(false)
const isDark = ref(false)
const searchQuery = ref('')

// 切换侧边栏
const toggleSidebar = () => {
  isCollapsed.value = !isCollapsed.value
}

// 切换主题
const toggleTheme = () => {
  isDark.value = !isDark.value
  document.documentElement.classList.toggle('dark', isDark.value)
}

// 处理用户菜单命令
const handleUserCommand = (command: string) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人资料功能开发中')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      userStore.logout()
      router.push('/login')
      ElMessage.success('已退出登录')
      break
  }
}
</script>

<style scoped>
.el-aside {
  position: fixed;
  left: 0;
  top: 0;
  height: 100vh;
  z-index: 1000;
}

.el-container {
  margin-left: v-bind('isCollapsed ? "64px" : "240px"');
  transition: margin-left 0.3s;
}

.el-menu {
  border-right: none;
}

.el-menu-item {
  margin: 4px 8px;
  border-radius: 6px;
}

.el-menu-item:hover {
  background-color: #f0f9ff;
  color: #1d4ed8;
}

.el-menu-item.is-active {
  background-color: #dbeafe;
  color: #1d4ed8;
  border-right: none;
}
</style>